//
//@File    :	15.13_newexcp.cpp
//@Time    :	2022/03/20 20:29:47
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	the bad_alloc exception
//
//使用new导致的内存分配问题
#include <iostream>
#include <new>
#include <cstdlib>//for exit() ,EXIT_FAILURE
using namespace std;

struct Big
{
    double stuff[20000];
};

int main()
{
    Big *pb;
    try
    {
        cout<<"Trying to get a big block of memory:\n";
        pb=new Big[10000];
        cout<<"Got past the new request:\n";
    }
    catch(bad_alloc & ba)
    {
        cout<<"Caught the excaption!\n";
        cout<<ba.what()<<endl;
        //what()方法返回字符串"std::bad_alloc"
        exit(EXIT_FAILURE);
    }
    cout<<"Memory successfully allocted\n";
    pb[0].stuff[0]=4;
    cout<<pb[0].stuff[0]<<endl;
    delete [] pb;
    system("pause");
    return 0;
}